import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/list',
    name: 'List',
    component: () => import(/* webpackChunkName: "list" */ '../views/List.vue')
  },
  {
    path: '/detail',
    name: 'Detail',
    component: () => import('../views/Detail.vue')
  },
  {
    path: '/detail/:id',
    name: 'DetailN',
    component: () => import('../views/Detail.vue')
  },
  {
    path: '/nested',
    name: 'Nested',
    redirect: '/nested/page-a', // 默认子路由
    component: () => import('../views/Nested.vue'),
    children: [
      { path: 'page-a', component: () => import('../views/NestedPages/PageA.vue') },
      { path: 'page-b', component: () => import('../views/NestedPages/PageB.vue') },
    ]
  },
  {
    path: '/permission',
    name: 'Permission',
    component: () => import('../views/Permission.vue')
  },
  {
    path: '/permission-detail',
    name: 'PermissionDetail',
    component: () => import('../views/PermissionDetail.vue'),
    beforeEnter: (to, from, next) => {
      // 路由独享的守卫
      // 这里可以进行路由级拦截
      // console.log(to)
      if (to.params.allow === undefined) {
        next('/permission')
      }
      next()
    }
  },
  {
    path: '*',
    name: '404',
    component: () => import('../views/404.vue')
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // 全局路由守卫
  // console.log('beforeEach', to, from, next)
  next()
})

export default router
